<!--#include file="./header.inc"-->
<%

select case aq
	case "save" 		: 	call save
	case "savebatch" 	: 	call saveBatch
	case "update"		: 	call update
	case "edit" 		: 	call edit
	case "editbatch" 	: 	call editBatch
	case "delete" 		: 	call delete
	case "checkname" 	: 	call checkName
	case else 			: 	call index
end select

call db.close()

function update
	dim isWrite
	isWrite = updateCache("config")
	if isWrite then
		admin.message "更新成功"
	else
		admin.message "更新失败"
	end if
end function

function index()
	sql = "select configID,configName,configValue,configRemark,isSystem from cj_config Order By configName asc,configID desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/config.index.html"--><%
end function

function edit()
	data = array("","","","false","")
	if iid > 0 then
		set rs = db.exec("select configName,configValue,configRemark,isSystem from cj_config where configID=" & iid, 1)
		if rs.eof then admin.message "id不存在！"
		data = rs
	end if
	%><!--#include file="./view/config.edit.html"--><%
end function

function save()
	dim configID, configName, configValue, configRemark, isSystem
	configName = admin.filter(request.form("configName"))
	if configName = "" then
		admin.message "变量名称不能为空"
	end if
	configValue = trim(request.form("configValue"))
	configRemark = trim(request.form("configRemark"))
	'isSystem = admin.isCheck(request.form("isSystem"))
	if iid > 0 then
		set rs = db.exec("select * from cj_config where configID=" & iid, 3)
		if rs.eof then admin.message "id" & iid & "不存在！"
	else
		if app.getConfig(configName) <> "" then
			admin.message "变量名称 " & configName & " 已经存在"
		end if
		set rs = db.exec("select * from cj_config", 3)
		rs.AddNew
	end if
	rs("configName") = configName
	rs("configValue") = configValue
	rs("configRemark") = configRemark
	'rs("isSystem") = isSystem
	rs.update
	rs.close : set rs = nothing
	updateCache "config"
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function editBatch()
	if batchId = "0" then admin.message "请选择ID！"
	sql = "select configID,configName,configValue,configRemark,isSystem from cj_config"
	sql = sql & " where configID in(" & batchId & ")" ''
	data = db.GetData(sql) '二维数组 第1维为字段，第2维为行数
	if not isArray(data) then admin.message "id不存在！"
	%><!--#include file="./view/config.editBatch.html"--><%
end function

function saveBatch()
	dim configID, configName, configValue, configRemark, isSystem, i, j, n
	if batchId = "0" then admin.message "请选择要修改的ID"
	configID = split(batchId, ",")
	j = 0
	n = 0
	for i = 0 to ubound(configID)
		iid = configID(i)
		configValue = trim(request.form("configValue_" & iid))
		set rs = db.exec("select configValue from cj_config where configID=" & iid, 3)
		if not rs.eof then
			rs("configValue") = configValue
			rs.update
			n = n + 1
		else
			j = j + 1
		end if
		rs.close
		set rs = nothing
	next
	updateCache "config"
	admin.back "修改成功！成功 " & n & " 条，失败 " & j & " 条", "返回列表", fileName & "?q=index"
end function

function delete()
	dim configName
	configName = admin.filter(request("configName"))
	if iid = 0 then app.json "id不可以为空", 1
	if configName = "" then app.json "名称不可以为空", 1
	call db.exec("Delete from cj_config where configID=" & iid, 1)
	app.appCacheDelete "config_" & configName
	'updateCache "config"
	app.json "删除成功", 0
end function

function checkName()
	dim iConfigName, nConfigName, configName
	iConfigName = admin.filter(request("configName"))
	if iConfigName = "" then
		app.json "名称不可以为空", 1
	end if
	nConfigName = app.isTest(iConfigName, "^([a-z_]+[\w]+)$")
	if isArray(nConfigName) then
		configName = nConfigName(0)
	else
		app.json "名称不合法，名称只能为字母或_开头的 字母+数字+_", 1
	end if

	sql = "select * from cj_config where configName='" & configName & "'"
	if iid > 0 then
		sql = sql & " and configID<>" & iid
	end if
	set rs = db.exec(sql, 1)
	if rs.eof then
		app.json "名称可以使用", 0
	else
		app.json "名称已经存在", 1
	end if
end function
%>